# -*- coding: UTF-8 -*-

"""

提币响应

监听消息队列 - coin.withdraw.response

    消息内容 seqno, status
    文本存储 逗号分隔

    status:
        0: 未处理
        1: 已完成
        2: 提币失败 (需要恢复账户余额)

"""


import pymongo, redis

# redis
rdb = redis.Redis(host='127.0.0.1', port=6379, db=0, decode_responses=True)

# mongodb
mdb = pymongo.MongoClient(host='127.0.0.1', port=27017, connect=False)

while 1:
    o = rdb.rpop("coin.withdraw.response")
    if not o:
        time.sleep(0.1)
        continue
    seqno, status = o.split(",")
    status = int(status)
    wd = mdb.exchange.withdraw.find_one({"seqno": seqno})
    mdb.exchange.withdraw.update_one({"seqno": seqno}, {"$set": {"status": status}})
    if status == 2:
        mdb.exchange.account.update_one({"userid": wd["userid"]}, {"$inc": {"coins." + wd["coin"] + ".balances": wd["amount"]}})
